using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using TanashCMS.Business;

public partial class _404 : CMSPageBase
{
    WebPage requestedPage = null;

    protected override void OnInit(EventArgs e)
    {
        string pagerequest = "";
        if (Request.QueryString["aspxerrorpath"] != null)
            pagerequest = Request.QueryString["aspxerrorpath"];
        else if (Request.Url.Query.Length >= 5)
        {
            if (Request.Url.Query.Substring(0, 5) == "?404;")
                pagerequest = Request.Url.Query.Substring(5);
        }

        if (pagerequest != "")
        {
            // see if we can't find a web page which matches the URL that was requested
            string pagename = Path.GetFileNameWithoutExtension(pagerequest);
            requestedPage = (WebPage)Configuration.GetCurrentSession().CreateQuery("select p from WebPage p where p.Title like '%" + pagename + "%' and p.Website.ID = " + mWebsite.ID.ToString()).SetFirstResult(0).SetMaxResults(1).UniqueResult();
            if (requestedPage != null)
            {
                //mCurrentPage = page;
                Response.Redirect("~/default.aspx?GUID=" + ((int)CMSObjectType.WebPage).ToString().PadLeft(2,'0') + requestedPage.GUID);
                //litMessage.Text = "Matching page:" + requestedPage.Title + ", " + requestedPage.GUID;
            }
        }

        

        base.OnInit(e);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (requestedPage != null)
            return;

        string referrer = "";
        if (Request.UrlReferrer != null)
            referrer = Request.UrlReferrer.ToString();
        bool searchReferral = false;
        bool insiteReferral = false;
        string message = "";
        string site = "";

        message += "<a href='" + Request.ApplicationPath + "'>Click here to return to the site home page</a></p>";
        message += "<hr/><p><strong>You may not be able to find the page you were after because of:</strong></p>";
        message += "<ol>";
        message += "<li>An <strong>out-of-date bookmark/favorite</strong></li>";
        message += "<li>A search engine that has an <strong>out-of-date listing for us</strong></li>";
        message += "<li>A <strong>mis-typed address</strong></li>";
        message += "</ol>";

        if (Request.UrlReferrer == null)
        {
        }
        else
        {
            if (referrer.IndexOf(".looksmart.co") > 0
                || referrer.IndexOf(".ifind.freeserve") > 0
                || referrer.IndexOf(".ask.co") > 0
                || referrer.IndexOf(".google.co") > 0
                || referrer.IndexOf(".altavista.co") > 0
                || referrer.IndexOf(".msn.co") > 0
                || referrer.IndexOf(".yahoo.co") > 0)
            {
                searchReferral = true;
                string[] query = Request.UrlReferrer.Query.TrimStart('?').Split('&');

                message = "<a href='" + Request.ApplicationPath.TrimEnd('/') + "'>Click here to return to the site home page</a></p>";
                message += "<hr/><p><strong>You may not be able to find the page you were after because of:</strong></p>";
                message += "<ol>";
                message += "<li>An <strong>out-of-date bookmark/favorite</strong></li>";
                message += "<li>A search engine that has an <strong>out-of-date listing for us</strong></li>";
                message += "<li>A <strong>mis-typed address</strong></li>";
                message += "</ol>";
                message += "<p>It looks like you came to our site through a search engine</p>";


            }

            message += "<p>For now, please click the <strong>Back</strong> button in your browser to return to the page you came from, or <a href='"+ Request.ApplicationPath + "'>click here to return to the home page of this site</a>.</p>";
        }
        litMessage.Text += message;
    }
}
